package com.example.sunsg.okhttptest.customview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import com.example.sunsg.okhttptest.util.Util;

public final class SwipeLayout extends FrameLayout {
    public static final boolean DEBUG = true;
    public static final String TAG = "SwipeLayout";
    public SwipeLayout(Context context) {
        super(context);
    }

    public SwipeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private boolean mIsSwipe = false;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean ret;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // No view catches ACTION_DOWN.
            // Force return true, otherwise no ACTION comes afterwards.
            ret = true;
        } else {
            if (!mIsSwipe) {
                // We catch ACTION_DOWN and afterwards ACTIONs.
                // Check swipe action here, not in onInterceptTouchEvent();
                dispatchSwipeEventIfNeeded(event.getX());
            }

            ret = mIsSwipe;
        }

        if (DEBUG) {
            Log.d(TAG, "onTouchEvent: " + event + " " + ret);
        }
        return ret;
    }

    private float mStartWidthThreshold;
    private float mSwipeWidthThreshold;
    private float mStartWidth = -1;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (DEBUG) {
            Log.d(TAG, "onInterceptTouchEvent: " + ev + " " + ev.getX());
        }

        if (mSwipeWidthThreshold == 0) {
            DisplayMetrics displayMetrics = Util.getDisplayMetrics(getContext());
            int width = displayMetrics.widthPixels;
            mStartWidthThreshold = width / 3; // Swipe starts from 0-33% Screen Width.
            mSwipeWidthThreshold = width / 4; // Swipe across 25% Screen Width.
        }

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (ev.getX() < mStartWidthThreshold) {
                mStartWidth = ev.getX();
            } else {
                mStartWidth = -1;
            }
        } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            dispatchSwipeEventIfNeeded(ev.getX());
        }

//            final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0;
        if (DEBUG) {
            Log.d(TAG, "onInterceptTouchEvent: " //+ disallowIntercept + " "
                    + mIsSwipe);
        }
        return mIsSwipe;
    }

    private void dispatchSwipeEventIfNeeded(float x) {
        if (isSwipeAction(x)) {
            mIsSwipe = true;
            mStartWidth = -1;

            dispatchSwipeEvent();
        }
    }

    private boolean isSwipeAction(float x) {
        return mStartWidth >= 0 &&
                x - mStartWidth > mSwipeWidthThreshold;
    }

    private SwipeListener mSwipeListener;
    public void setSwipeListener(SwipeListener swipeListener) {
        mSwipeListener = swipeListener;
    }
    private void dispatchSwipeEvent() {
        if (mSwipeListener != null) {
            mSwipeListener.onSwipe();
        }
    }
    public interface SwipeListener {
        void onSwipe();
    }
}